import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hotfil_pub_web/shared/sputils.dart';

import 'en_US.dart';
import 'zh_TW.dart';

class TranslationService extends Translations {
  static get locale {
    var spLang = SpUtil().getStr('lang');
    if (spLang == null) {
      //
      String deviceLocaleStr = Get.deviceLocale.toString();
      if (deviceLocaleStr.contains('zh')) {
        SpUtil().setStr('lang', 'zh_TW');
        return const Locale('zh', 'TW');
      } else {
        SpUtil().setStr('lang', 'en_US');
        return const Locale('en', 'US');
      }
    } else {
      if (spLang.contains('zh')) {
        SpUtil().setStr('lang', 'zh_TW');
        return const Locale('zh', 'TW');
      } else {
        SpUtil().setStr('lang', 'en_US');
        return const Locale('en', 'US');
      }
    }
  }

  static final fallbackLocale = Locale('en', 'US');
  @override
  Map<String, Map<String, String>> get keys => {
        'en_US': en_US,
        'zh_TW': zh_TW,
      };

  static get isTwLang => Get.locale.toString() == 'zh_TW';
  static get isENLang => Get.locale.toString() == 'en_US';
  static void updateLocaleToTW() {
    SpUtil().setStr('lang', 'zh_TW');

    Get.updateLocale(Locale('zh', 'TW'));
  }

  static void updateLocaleToEN() {
    SpUtil().setStr('lang', 'en_US');
    Get.updateLocale(Locale('en', 'US'));
  }
}
